import time
import uuid

from constant.BlogConstant import BlogConstant
from entity.models import User
from entity.models import db


def get_user_by_uuid(uuid):
    return db.session.query(User).filter(User.uuid == uuid).first()


def get_user_by_account(account, account_type):
    return db.session.query(User).filter(User.type == account_type).filter(User.third_part_account == account).first()


def assemble_user_qq_login(user_info, openid):
    user = User()
    user.uuid = uuid.uuid4()
    user.real_name = ''
    user.nick_name = user_info['nickname']
    user.password = ''
    user.email = ''
    user.salt = ''
    user.account = ''
    user.is_admin = 0
    user.sex = user_info['gender']
    user.create_at = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
    user.create_by = user.uuid
    user.update_at = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
    user.update_by = user.uuid
    user.status = BlogConstant.user_status_normal
    user.type = BlogConstant.user_type_qq
    user.third_part_account = openid
    user.avatar = user_info['figureurl_qq_1']
    return user


def save_user_by_qq(user):
    db.session.add(user)
    db.session.commit()
    db.session.close()
